<?php
class item_viewwarning extends template_item
{
	var $default_name = "View Other Warning Stauts";
	var $default_description = "Use this item to other member's warnings.";
	
	var $allow_multiple = 1;
	
	var $item_config = array();
	
	function load_item(&$item)
	{
		parent::load_item($item);
		
		if($item['item_config'] || $item['item_id'] != 0)
		{
			$this->item_config = unserialize($item['item_config']);
		}
		else
		{
			$this->item_config['protected'] = array($this->ipsclass->vars['admin_group']);
		}
	}
	
	function admin_form()
	{
		if(!$this->ipsclass->vars['warn_on'])
		{
			$this->ipsclass->admin->error("ITEM ERROR: You must enable the warning system to use this item");
		}
		
		$groups = array();
		foreach($this->ipsclass->cache['group_cache'] as $g_id => $group)
		{
			$groups[] = array($g_id,$group['g_title']);
		}
		
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"<b>Which groups are protected from this item?</b>",
															$this->ipsclass->adskin->form_multiselect("item[".$this->item['item_id']."][protected][]", $groups, $this->item_config['protected']),
													)	);
		
		return $html;
	}
	
	function admin_save(&$item_info, $input)
	{
		if(!$this->ipsclass->vars['warn_on'])
		{
			$this->ipsclass->admin->error("ITEM ERROR: You must enable the warning system to use this item");
		}
		
		$save = array();
		$save['protected'] = $input['protected'] ? $input['protected'] : array();
		
		$item_info['item_config'] = serialize($save);
	}
	
	function public_use_form($inventory_ids = array())
	{
		$this->ipsclass->load_language('lang_market_items');
		
		if($this->ipsclass->input['member_name'])
		{
			$member = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'members','where'=>"members_l_display_name='".strtolower($this->ipsclass->input['member_name'])."'"));
			if(!$member['id'])
			{
				$this->ipsclass->Error(array('MSG'=>'no_user'));
			}
			
			if(in_array($member['mgroup'],$this->item_config['protected']))
			{
				$this->ipsclass->Error(array('MSG'=>'protected_user'));
			}
			
			$warning = "";
			if($this->ipsclass->vars['warn_show_rating'] == 0)
			{
				if ( $member['warn_level'] < 1 )
				{
					$member['warn_img'] = '<{WARN_0}>';
					$member['warn_percent'] = 0;
				}
				else if ( $member['warn_level'] >= $this->ipsclass->vars['warn_max'] )
				{
					$member['warn_img']     = '<{WARN_5}>';
					$member['warn_percent'] = 100;
				}
				else
				{
					$member['warn_percent'] = $member['warn_level'] ? sprintf( "%.0f", ( ($member['warn_level'] / $this->ipsclass->vars['warn_max']) * 100) ) : 0;
					
					if ( $member['warn_percent'] > 100 )
					{
						$member['warn_percent'] = 100;
					}
					
					if ( $member['warn_percent'] >= 81 )
					{
						$member['warn_img'] = '<{WARN_5}>';
					}
					else if ( $member['warn_percent'] >= 61 )
					{
						$member['warn_img'] = '<{WARN_4}>';
					}
					else if ( $member['warn_percent'] >= 41 )
					{
						$member['warn_img'] = '<{WARN_3}>';
					}
					else if ( $member['warn_percent'] >= 21 )
					{
						$member['warn_img'] = '<{WARN_2}>';
					}
					else if ( $member['warn_percent'] >= 1 )
					{
						$member['warn_img'] = '<{WARN_1}>';
					}
					else
					{
						$member['warn_img'] = '<{WARN_0}>';
					}
				}
				
				$warning = "(".$member['warn_percent']."%) ".$member['warn_img'];
			}
			else
			{
				$warning = "&lt;&nbsp;".$this->ipsclass->vars['warn_min']." ( ".$member['warn_level']." ) ".$this->ipsclass->vars['warn_max']."&nbsp;&gt;";
			}
			
			$this->delete_items($inventory_ids);
			
			$inv_id = reset($inventory_ids);
			$this->_write_log($inv_id, "Viewed <a href='{$this->ipsclass->base_url}showuser={$member['id']}'>{$member['members_display_name']}</a> warning level");
			
			return <<<EOF
<div class='formsubtitle'>{$this->ipsclass->lang['view_warning']}</div>
<table class='ipbtable' cellspacing='0' cellpadding='0'>
	<tr>
		<td class='row1' align='left'>
			{$this->ipsclass->lang['member_name']}
		</td>
		<td class='row2' align='right'>
			<a href='{$this->ipsclass->base_url}showuser={$member['id']}'>{$member['members_display_name']}</a>
		</td>
	</tr>
	<tr>
		<td class='row1' align='left'>
			{$this->ipsclass->lang['warning_level']}
		</td>
		<td class='row2' align='right'>
			{$warning}
		</td>
	</tr>
</table>
EOF;
		}
		else
		{
			$hidden_fields = $this->_get_hidden_fields($inventory_ids);
			
			return <<<EOF
<script type="text/javascript" src='jscripts/ipb_xhr_findnames.js'></script>
<div id='ipb-get-members' style='border:1px solid #000; background:#FFF; padding:2px;position:absolute;width:210px;display:none;z-index:1'></div>
<div class='formsubtitle'>{$this->ipsclass->lang['view_warning']}</div>
<form action='{$this->base_url}' method='post' id='changeform'>
{$hidden_fields}
	<table class='ipbtable' cellspacing='0' cellpadding='0'>
		<tr>
			<td class='row1' align='left'>
				<b>{$this->ipsclass->lang['which_member']}</b>
			</td>
			<td class='row2' align='right'>
				<input type='input' id='member_name' name='member_name' value='' size='30' class='forminput' autocomplete='off' style='width:210px' />
			</td>
		</tr>
		<tr>
			<td class='darkrow1' align='center' colspan='2'>
				<input type='submit' value='{$this->ipsclass->lang['view_warning']}' class='forminput' />
			</td>
		</tr>
	</table>
</form>
<script type="text/javascript">
	// INIT find names
	init_js( 'changeform', 'member_name');
	// Run main loop
	setTimeout( 'main_loop()', 10 );
</script>
EOF;

		}
	}
	
	function input_done()
	{
		return false;
	}
}
?>